<template>
	<cu-custom :isBack="true">
		<template #title>报告生成记录</template>
	</cu-custom>
	
	<view class="theme-shadow mb-2">
		<view class="report-row" v-for="(item, index) in dataList" :key="index" @click="proviewDoc(item)">
			<text class="id-row">【编号{{ item.id }}】</text>
			<!-- <MidEllipsis :text="item.title" /> -->
			<text class="text-ellipsis-1 mid-ellipsis" :style="{
				color: item.filepath ? '#3974df' : '#999'
			}">{{ item.title }}</text>
			<text class="cuIcon-right text-gray"></text>
		</view>
	</view>
	<view v-if="dataList.length === 0" class="empty-hint">
		<image src="../../static/images/empty-message.png" mode="aspectFit"></image>
		<view class="text-gray small-text">当前没有报告生成记录</view>
	</view>
	<view v-else class="text-gray text-center">
		没有更多了
	</view>
</template>

<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app"
import { reactive, ref, type Ref } from 'vue';
import { getReport } from "@/api/mission.js"
import path from "path-browserify"
import { getSuffix } from "@/utils/base"
import MidEllipsis from "@/components/MidEllipsis.vue"
import { router } from "@/router" 
import store from "@/store/index.js"

interface IReport {
	id: number
	title: string
	filepath: string
	suffix: string 
}
interface IFile {
	name: string
	url: string
}

const props = defineProps({
	mission_id: {
		type: String,
		default: null
	}
})

let mission_id = null
const dataList:Ref<IReport[]> = ref([])

const proviewDoc = (e: IReport) => {
	if (e.filepath) {
		if (store.getters.osName === 'ios') {
			router.push({
				path: "/pages/index/word",
				params: {
					title: e.title,
					filepath: e.filepath
				}
			})
		} else {
			location.href = e.filepath
		}
	} else {
		uni.showToast({
			icon: "none",
			title: "报告生成中 请稍后查看"
		})
	}
}

const loadData = () => {
	getReport(mission_id)
		.then((res) => {
			res.map((data: any) => {
				let reportFile: IFile = {
					name: null,
					url: null
				}
				if (data.reportFiles) {
					reportFile = JSON.parse(data.reportFiles)
				}
				console.log(reportFile.name)
				dataList.value.push({
					id: data.id,
					title: reportFile.name ? path.basename(reportFile.name) : "报告生成中",
					suffix: reportFile.name ? getSuffix(reportFile.name) : null,
					filepath: reportFile.url
				})
			})
		})
}

onLoad((options: any) => {
	if (options.mission_id) {
		mission_id = options.mission_id
		loadData()
	}
})

</script>

<style lang="stylus" scoped>
body
	background-color #fff
.report-row
	display: flex
	align-items: center
	padding: 30rpx 40rpx
	justify-content: left
	border-bottom: 2rpx solid #eeeeee
	background-color #ffffff
	position: relative;
	overflow: hidden
	width: 750rpx
	font-size 34rpx
	.id-row
		color: #2a2a2a
		white-space: nowrap
		width: 180rpx
	.mid-ellipsis
		width: 450rpx
.report-row:active
	background-color #efefef
	
.empty-hint
	text-align: center
</style>
